/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.BorrowreturnMessage;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.BorrowreturnMessageRepository;
import me.zhengjie.service.BorrowreturnMessageService;
import me.zhengjie.service.dto.BorrowreturnMessageDto;
import me.zhengjie.service.dto.BorrowreturnMessageQueryCriteria;
import me.zhengjie.service.mapstruct.BorrowreturnMessageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author ...
* @date 2020-11-07
**/
@Service
@RequiredArgsConstructor
public class BorrowreturnMessageServiceImpl implements BorrowreturnMessageService {

    private final BorrowreturnMessageRepository borrowreturnMessageRepository;
    private final BorrowreturnMessageMapper borrowreturnMessageMapper;

    @Override
    public Map<String,Object> queryAll(BorrowreturnMessageQueryCriteria criteria, Pageable pageable){
        Page<BorrowreturnMessage> page = borrowreturnMessageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(borrowreturnMessageMapper::toDto));
    }

    @Override
    public List<BorrowreturnMessageDto> queryAll(BorrowreturnMessageQueryCriteria criteria){
        return borrowreturnMessageMapper.toDto(borrowreturnMessageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BorrowreturnMessageDto findById(Integer borrowreturnMessageId) {
        BorrowreturnMessage borrowreturnMessage = borrowreturnMessageRepository.findById(borrowreturnMessageId).orElseGet(BorrowreturnMessage::new);
        ValidationUtil.isNull(borrowreturnMessage.getBorrowreturnMessageId(),"BorrowreturnMessage","borrowreturnMessageId",borrowreturnMessageId);
        return borrowreturnMessageMapper.toDto(borrowreturnMessage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BorrowreturnMessageDto create(BorrowreturnMessage resources) {
        return borrowreturnMessageMapper.toDto(borrowreturnMessageRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BorrowreturnMessage resources) {
        BorrowreturnMessage borrowreturnMessage = borrowreturnMessageRepository.findById(resources.getBorrowreturnMessageId()).orElseGet(BorrowreturnMessage::new);
        ValidationUtil.isNull( borrowreturnMessage.getBorrowreturnMessageId(),"BorrowreturnMessage","id",resources.getBorrowreturnMessageId());
        borrowreturnMessage.copy(resources);
        borrowreturnMessageRepository.save(borrowreturnMessage);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer borrowreturnMessageId : ids) {
            borrowreturnMessageRepository.deleteById(borrowreturnMessageId);
        }
    }

    @Override
    public void download(List<BorrowreturnMessageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BorrowreturnMessageDto borrowreturnMessage : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("持书者id ", borrowreturnMessage.getFromReaderId());
            map.put("借书者id ", borrowreturnMessage.getToReaderId());
            map.put("实体书", borrowreturnMessage.getRealBookId());
            map.put("快递寄送编号 ", borrowreturnMessage.getDeliveryId());
            map.put("发信息时的时间 ", borrowreturnMessage.getMessageDate());
            map.put("0:未读 1:已读 ", borrowreturnMessage.getStatus());
            map.put("消息内容 ", borrowreturnMessage.getContent());
            map.put("借书者的地址 ", borrowreturnMessage.getAddress());
            map.put("借书者预约取书时间 ", borrowreturnMessage.getTime());
            map.put("借书者的电话号码 ", borrowreturnMessage.getPhone());
            map.put("0：自提 1：物流 2：书易配送", borrowreturnMessage.getType());
            map.put("多本书并物流订单", borrowreturnMessage.getBookcombine());
            map.put("是否启用 1启用 0未启用", borrowreturnMessage.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}